package com.itheima.stock.controller.v1;

import com.alibaba.excel.EasyExcel;
import com.itheima.stock.api.StockControllerAPI;
import com.itheima.stock.constant.Constans;
import com.itheima.stock.dto.StockInfoExcelDTO;
import com.itheima.stock.entity.StockBusiness;
import com.itheima.stock.response.PageResult;
import com.itheima.stock.response.ResponseResult;
import com.itheima.stock.service.StockService;
import com.itheima.stock.vo.*;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RestController;


import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @文件名: StockContorller
 * @作者: DX
 * @创建日期: 2025/10/23
 * @描述:
 */
@RestController
public class StockContorller implements StockControllerAPI {

    private final StockService stockService;

    public StockContorller(StockService stockService) {
        this.stockService = stockService;
    }

    @Override
    public ResponseResult<FileDownloadVO> export(int page, int pageSize, HttpServletResponse response) {
        //1.设置响应数据的类型:excel
        response.setContentType("application/vnd.ms-excel");
        //2.设置响应数据的编码格式
        response.setCharacterEncoding("utf-8");

        AtomicInteger counter = new AtomicInteger(0);

        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(Constans.EXCEL_FILE_PREFIX + page + Constans.EXCEL_FILE_SUFFIX, StandardCharsets.UTF_8));

        try {
            EasyExcel.write(response.getOutputStream(), StockInfoExcelDTO.class)
                    .sheet(Constans.EXCEL_FILE_PREFIX + page + Constans.EXCEL_FILE_SUFFIX)
                    .doWrite(
                            stockService.stockPage(page, pageSize).getRows().stream().map(moreVO -> {
                                StockInfoExcelDTO dto = new StockInfoExcelDTO();
                                counter.set(counter.incrementAndGet());
                                return dto;
                            }).toList());
        } catch (IOException e) {
            throw new RuntimeException(e.getCause());
        }
        return ResponseResult.ok(FileDownloadVO.builder()
                .fileName(URLEncoder.encode(Constans.EXCEL_FILE_PREFIX + page + Constans.EXCEL_FILE_SUFFIX))
                .build());

    }

    @Override
    public ResponseResult<UpDownVO<OptionVO>> upDownCount() {
        return ResponseResult.ok(stockService.upDownCount());
    }

    @Override
    public ResponseResult<PageResult<MoreVO>> stockPage(Integer page, Integer pageSize) {
        return ResponseResult.ok(stockService.stockPage(page, pageSize));
    }

    @Override
    public ResponseResult<List<StockUpdownDomainVO>> stockIncreaseLimit() {
        return ResponseResult.ok(stockService.stockIncreaseLimit());
    }

    @Override
    public ResponseResult<List<StockBlockRtInfoVO>> sectorAllLimit() {

        return ResponseResult.ok(stockService.sectorAllLimit());
    }

    @Override
    public ResponseResult<List<MarketIndexVO>> innerIndexAll() {
        return ResponseResult.ok(stockService.innerIndexAll());
    }

    @Override
    public ResponseResult<List<StockBusiness>> getStockBusiness() {

        List<StockBusiness> stockBusiness = stockService.getStockBusiness();

        return ResponseResult.ok(stockBusiness);
    }
}
